// order//order/code/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 0,
    expireTime: '',
    name: '',
    orderInfo: {},
    timer: '',
    remark: ''
  },

  getOrderInfo: function(oid){
    let that = this;
    wx.request({
      url: app.globalData.host+ '/api/order/GetOrderCheck?oid=' + oid,
      success: function(res){
        if(res.data.Code == 0){
          if(res.data.Data){
            let orderInfo = res.data.Data;
            orderInfo.codesnList = [];
            let codelist = orderInfo.CodeSN.split(",");
            let lengthIndex = codelist.length;
            for(let m = lengthIndex-1; m >= 0; m--){
              orderInfo.codesnList.push(codelist[m]);
            }
            if(orderInfo.codesnList.length>0){
              let list = orderInfo.codesnList;
              for(let i = 0; i < list.length; i++){
                list[i] = list[i].replace(/\s/g,'').replace(/(.{4})/g,"$1 ");
              }
              orderInfo.canUse = list.length - orderInfo.CheckCount;
            }
            that.setData({
              orderInfo: orderInfo
            })
          }
        }
      }
    })
  },
  getOrderFresh: function(oid){
    let that = this;
    var timesRun = 0;
    var timer = setInterval(function(){
      timesRun += 1;
      if (timesRun === 20) {
        clearInterval(timer);
      }
      wx.request({
        url: app.globalData.host+ '/api/order/GetOrderCheck?oid=' + oid,
        success: function(res){
          if(res.data.Code == 0){
            if(res.data.Data){
              let orderInfo = res.data.Data;
              orderInfo.codesnList = [];
              let codelist = orderInfo.CodeSN.split(",");
              let lengthIndex = codelist.length;
              for(let m = lengthIndex-1; m >= 0; m--){
                orderInfo.codesnList.push(codelist[m]);
              }
              if(orderInfo.codesnList.length>0){
                let list = orderInfo.codesnList;
                for(let i = 0; i < list.length; i++){
                  list[i] = list[i].replace(/\s/g,'').replace(/(.{4})/g,"$1 ");
                }
                orderInfo.canUse = list.length - orderInfo.CheckCount;
                if(orderInfo.canUse == 0){
                  clearInterval(timer);
                }
              }
              that.setData({
                orderInfo: orderInfo
              })
            }
          }
        }
      })
    },3000)
    that.setData({
      timer: timer
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.name){
      this.setData({
        name: options.name,
        expireTime: options.expiretime
      })
    }
    if(options.type){
      if(options.type == 2){
        this.getOrderDetail(options.oid);
      }
      this.setData({
        type: options.type
      })
    }
    this.getOrderInfo(options.oid);
    this.getOrderFresh(options.oid);
  },
  getOrderDetail: function(oid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/order/GetOrder?uid=' + app.globalData.userInfo.Uid + '&oid=' + oid,
      success: function(res) {
        if (res.data.Code == 0) {
          let info = res.data.Data;
          let remark = info.OrderInfo.BuyerRemark;
          that.setData({
            remark: remark
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },
  
  onUnload: function() {
    clearInterval(this.data.timer);
  },
})